package com.it.gw.modules.water.activity.rv

import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.activity.viewModels
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.it.common.bean.MapDataBean
import com.it.common.utils.GsonUtils
import com.it.common.utils.isNotEmpty
import com.it.data.network.req.RiverRecordDto
import com.it.gw.R
import com.it.gw.base.BaseActivity
import com.it.gw.databinding.ActivityRvitemdetailBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.manager.DictionariesManager
import com.it.gw.manager.DictionariesManager.BANK
import com.it.gw.manager.DictionariesManager.CR_OVER_TYPE
import com.it.gw.manager.DictionariesManager.RV_GRAD
import com.it.gw.manager.DictionariesManager.RV_TYPE
import com.it.gw.modules.map.activity.MapActivity
import com.it.gw.modules.water.viewmodel.WaterViewModel

//水库详情
class RvItemDetailActivity : BaseActivity<ActivityRvitemdetailBinding>() {
    private val waterViewModel by viewModels<WaterViewModel>()

    companion object {
        const val TAG = "ResItemDetailActivity"
    }


    override fun initObserver() {
        waterViewModel.riverRecordDtoDetailLiveData.observe(this) {
            ProgressDialog.hide()
            Log.d(TAG, "resDetailLiveData: ${it}")
            if (it.result && it.data.isNotEmpty()) {
                setDetailView(it.data!!)
            }

        }
        waterViewModel.insertLiveData.observe(this) {
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }

        }

    }

    private fun setDetailView(data: RiverRecordDto) {
        binding.lgtd.text = data.lgtd
        binding.lttd.text = data.lttd
        binding.rvCode.setText(data.rvCode)
        binding.rvName.setText(data.rvName)
        binding.rvSourLoc.setText(data.rvSourLoc)
        binding.rvMouLoc.setText(data.rvMouLoc)
//        binding.crOverType.setText(data.crOverType)

        //跨界类型
        binding.crOverType.setText(
            DictionariesManager.findNameByCode(
                list = DictionariesManager.dictionariesMap.get(CR_OVER_TYPE),
                code = data.crOverType
            )
        )


        binding.folwArea.setText(data.folwArea)

//        binding.rvType.setText(data.rvType)

        //河流类型
        binding.rvType.setText(
            DictionariesManager.findNameByCode(
                list = DictionariesManager.dictionariesMap.get(RV_TYPE),
                code = data.rvType
            )
        )


        // binding.rvGrad.setText(data.rvGrad)

        //河流级别
        binding.rvGrad.setText(
            DictionariesManager.findNameByCode(
                list = DictionariesManager.dictionariesMap.get(RV_GRAD),
                code = data.rvGrad
            )
        )

//        binding.bank.setText(data.bank)


        //岸别
        binding.bank.setText(
            DictionariesManager.findNameByCode(
                list = DictionariesManager.dictionariesMap.get(BANK),
                code = data.bank
            )
        )

        binding.rvLen.setText(data.rvLen)
        binding.evBasArea.setText(data.evBasArea)
        binding.lonAverAnnFlow.setText(data.lonAverAnnFlow)
        binding.averSlop.setText(data.averSlop)
        //现状管理单位，缺
        binding.note.setText(data.note)


    }


    override fun initListener() {

        binding.lgtd.setOnClickListener {
            startLocation()
        }
        binding.lttd.setOnClickListener {
            startLocation()
        }
        binding.btnSave.setOnClickListener {
            insertRiverRecord()

        }
        binding.rvType.setOnClickListener {
            DictionariesManager.dictionariesMap.get(RV_TYPE)?.let { it1 ->
                showDictionariesWheelViewDialog(
                    dataList = it1,
                    okAction = { index ->
                        binding.rvType.text =
                            DictionariesManager.dictionariesMap.get(RV_TYPE)!![index].ITEMNAME

                    })
            }
        }
        binding.rvGrad.setOnClickListener {
            DictionariesManager.dictionariesMap.get(RV_GRAD)?.let { it1 ->
                showDictionariesWheelViewDialog(
                    dataList = it1,
                    okAction = { index ->
                        binding.rvGrad.text =
                            DictionariesManager.dictionariesMap.get(RV_GRAD)!![index].ITEMNAME

                    })
            }
        }
        binding.bank.setOnClickListener {
            DictionariesManager.dictionariesMap.get(BANK)?.let { it1 ->
                showDictionariesWheelViewDialog(
                    dataList = it1,
                    okAction = { index ->
                        binding.bank.text =
                            DictionariesManager.dictionariesMap.get(BANK)!![index].ITEMNAME

                    })
            }
        }
        binding.crOverType.setOnClickListener {
            DictionariesManager.dictionariesMap.get(CR_OVER_TYPE)?.let { it1 ->
                showDictionariesWheelViewDialog(
                    dataList = it1,
                    okAction = { index ->
                        binding.crOverType.text =
                            DictionariesManager.dictionariesMap.get(CR_OVER_TYPE)!![index].ITEMNAME

                    })
            }
        }


    }

    private fun insertRiverRecord() {
        ProgressDialog.show(this)
        val dto = RiverRecordDto(
            lgtd = binding.lgtd.text.toString(),
            lttd = binding.lttd.text.toString(),
            rvCode = binding.rvCode.text.toString(),
            rvName = binding.rvName.text.toString(),
            rvSourLoc = binding.rvSourLoc.text.toString(),
            rvMouLoc = binding.rvMouLoc.text.toString(),

//            crOverType = binding.crOverType.text.toString(),
            crOverType = DictionariesManager.findCodeByName(
                list = DictionariesManager.dictionariesMap.get(CR_OVER_TYPE),
                name = binding.crOverType.text.toString()
            ),


            folwArea = binding.folwArea.text.toString(),
          //  rvType = binding.rvType.text.toString(),

            rvType = DictionariesManager.findCodeByName(
                list = DictionariesManager.dictionariesMap.get(RV_TYPE),
                name = binding.rvType.text.toString()
            ),


//            rvGrad = binding.rvGrad.text.toString(),
            rvGrad = DictionariesManager.findCodeByName(
                list = DictionariesManager.dictionariesMap.get(RV_GRAD),
                name = binding.rvGrad.text.toString()
            ),

           // bank = binding.bank.text.toString(),
            bank = DictionariesManager.findCodeByName(
                list = DictionariesManager.dictionariesMap.get(BANK),
                name = binding.bank.text.toString()
            ),
            rvLen = binding.rvLen.text.toString(),
            evBasArea = binding.evBasArea.text.toString(),
            lonAverAnnFlow = binding.lonAverAnnFlow.text.toString(),
            averSlop = binding.averSlop.text.toString(),
            note = binding.note.text.toString()
        )
        waterViewModel.insertRiverRecord(dto)

    }

    override fun initData() {
        ProgressDialog.show(this)
        getRiveBasic()
    }

    private fun getRiveBasic() {
        waterViewModel.getRiveBasic(intent.getStringExtra("id")!!)

    }


    var title = ""
    override fun initView() {
        DictionariesManager.getHeliuDictionaries()
        title = "${intent.getStringExtra("title")}"
        setTitle(
            title = title,
            isBack = true,
            tvRightRes1 = R.string.str_location,
            tvRight1Action = {
                val intent = Intent(this, MapActivity::class.java)
                intent.putExtra("title", title)
                val mapList = mutableListOf(
                    MapDataBean(
                        name = title,
                        lgtd = binding.lgtd.text.toString(),
                        lttd = binding.lttd.text.toString(),
                    )
                )
                intent.putExtra(MapActivity.MAP_DATA, GsonUtils.objToJsonStr(mapList))
                startActivity(intent)
            }

        )

    }

    override fun setContentView() {
        binding = ActivityRvitemdetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    //声明AMapLocationClientOption对象
    private var mLocationOption: AMapLocationClientOption? = null

    //声明AMapLocationClient类对象
    private var mLocationClient: AMapLocationClient? = null
    private fun startLocation() {
        Log.d(TAG, "startLocation: ===========")
        mLocationClient = AMapLocationClient(applicationContext)
        mLocationClient?.setLocationListener(object : AMapLocationListener {
            override fun onLocationChanged(p0: AMapLocation?) {
                val longitude = "${p0?.longitude}"
                val latitude = "${p0?.latitude}"
                Log.d(TAG, "onLocationChanged: ${longitude} ${latitude}")
                binding.lttd.setText(latitude)
                binding.lgtd.setText(longitude)

            }

        })

        mLocationOption = AMapLocationClientOption()
        val option = AMapLocationClientOption()
        option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn)
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setOnceLocation(true);
        option.setOnceLocationLatest(true);

        if (null != mLocationClient) {
            mLocationClient?.setLocationOption(option)
            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
            mLocationClient?.stopLocation()
            mLocationClient?.startLocation()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mLocationClient?.onDestroy();//销毁定位客户端，同时销毁本地定位服务。
    }


}